# Figure 8
# By race and age

byrace <- imps_race
y <- seq(1989,2022,by = 3)

# Average impute results in each year
g_white <- sapply(1:length(y),function(i){Reduce("+",lapply(1:5, function(k){byrace[[i]]$white[[k]]$Gini}))/5})
g_black<- sapply(1:length(y),function(i){Reduce("+",lapply(1:5, function(k){byrace[[i]]$black[[k]]$Gini}))/5})

byage <- imps_age
y <- seq(1989,2022,by = 3)
g_work <- sapply(1:length(y),function(i){Reduce("+",lapply(1:5, function(k){byage[[i]]$working[[k]]$Gini}))/5})
g_ret <- sapply(1:length(y),function(i){Reduce("+",lapply(1:5, function(k){byage[[i]]$retired[[k]]$Gini}))/5})

# Plot by race

G <- data.frame(c(y[1:12]),g_white,g_black)
colnames(G) <- c("Year","White","Black")
G <- reshape2::melt(G, id.var = "Year")

sz <- 2
gini_plot <- ggplot(G,aes(x=Year,y=value,colour = variable,shape = variable)) +
  geom_line(size = sz) +
  geom_point(size = 7.5)+
  scale_color_manual(name = "",
                     labels = c("White","Black"),
                     values=c("#004488","#6699cc")) + 
  scale_shape_manual(name = "",
                     labels = c("White","Black"),
                     values = c(17,16)) +
  xlab("Year") + ylab("Gini Index")+  labs(color='')  +
  coord_cartesian(ylim = c(0.62, 0.76)) +
  theme(panel.border = element_rect(colour = "black", fill=NA, size=0.5),
        panel.background = element_rect(fill = 'white'),
        panel.grid = element_blank(),
        panel.grid.major.y = element_line(color = "gray"),
        panel.grid.minor.y = element_line(color = "gray"),
        legend.position=c(0.15,0.9),
        legend.background = element_blank(),
        legend.key = element_rect(fill = 'transparent'),
        text = element_text(family = "serif",size=30)) + ylab("Gini Index") +
  scale_x_continuous(breaks = y[y%%2==0],labels = y[y%%2==0])
ggsave(plot = gini_plot, width = 8, height = 6, filename = "fig8a.png")

# Plot by age
G <- data.frame(c(y[1:12]),g_work,g_ret)
colnames(G) <- c("Year","Working","Retired")
G <- reshape2::melt(G, id.var = "Year")

sz <- 2
gini_plot <- ggplot(G,aes(x=Year,y=value,colour = variable,shape = variable)) +
  geom_line(size = sz) +
  geom_point(size = 7.5)+
  scale_color_manual(name = "",
                     labels = c("Working","Retiring"),
                     values=c("#994455","#ee99aa")) + 
  scale_shape_manual(name = "",
                     labels = c("Working","Retiring"),
                     values = c(17,16)) +
  coord_cartesian(ylim = c(0.63, 0.78)) +
  xlab("Year") + ylab("")+  labs(color='')  +
  theme(panel.border = element_rect(colour = "black", fill=NA, size=0.5),
        panel.background = element_rect(fill = 'white'),
        panel.grid = element_blank(),
        panel.grid.major.y = element_line(color = "gray"),
        panel.grid.minor.y = element_line(color = "gray"),
        legend.position=c(0.15,0.9),
        legend.background = element_blank(),
        legend.key = element_rect(fill = 'transparent'),
        text = element_text(family = "serif",size=30)) +
  scale_x_continuous(breaks = y[y%%2==0],labels = y[y%%2==0])
ggsave(plot = gini_plot, width = 8, height = 6, filename = "fig8b.png")






